#include"stdafx.h"

int plot::set_IO_options()
{
	IO_options["croprot"]["READ_FROM_FILE"]=0;
	IO_options["croprot"]["STORE_VALUES"]=0;		
	IO_options["fertrot"]["READ_FROM_FILE"]=0;
	IO_options["fertrot"]["STORE_VALUES"]=0;
	IO_options["n2o_day"]["READ_FROM_FILE"]=1;
	IO_options["n2o_day"]["STORE_VALUES"]=1;	
	IO_options["n2o_mon"]["READ_FROM_FILE"]=1;
	IO_options["n2o_mon"]["STORE_VALUES"]=1;
	IO_options["n2o_year"]["READ_FROM_FILE"]=1;
	IO_options["n2o_year"]["STORE_VALUES"]=1;
	IO_options["n2o_EF_site"]["READ_FROM_FILE"]=1;
	IO_options["n2o_EF_site"]["STORE_VALUES"]=1;
	IO_options["n2o_EF_plot"]["READ_FROM_FILE"]=1;
	IO_options["n2o_EF_plot"]["STORE_VALUES"]=1;
	IO_options["met_mon"]["READ_FROM_FILE"]=0;
	IO_options["met_mon"]["STORE_VALUES"]=1;
	IO_options["met_mon_lt"]["READ_FROM_FILE"]=0;
	IO_options["me_mon_lt"]["STORE_VALUES"]=1;
	
	IO_options["ETP_mon_lt"]["READ_FROM_FILE"]=0;
	IO_options["ETP_mon_lt"]["STORE_VALUES"]=1;
	IO_options["ETP_mon"]["READ_FROM_FILE"]=0;
	IO_options["ETP_mon"]["STORE_VALUES"]=1;
	return 0;
}
int plot::read_plotprops_from_list2d(list2d &l,int row,string inroot)
{
	timestamp startdate;
	timestamp enddate;
	name=l.row_id[row];
	root="./"+inroot+"/"+name+"/";
	
	string variable_str;
	map<  string,vector <double> >::const_iterator it;
        cout<<endl<<root<<name<<endl;
	for(it=l.var.begin();it!=l.var.end();it++)
	{
		variable_str=it->first;
		this->plotprops[variable_str]=it->second[row];
                cout<<variable_str<<"\t"<<this->plotprops[variable_str]<<"\t";
	}
    cout<<endl;
	return 0;
}
int plot::set_inputs(list2d *croplist, list2d *fertlist)
{
	vector <string> variables;
	inp_measurements *mother=NULL;
	inp_tempmap_aggr *motheraggr= new inp_tempmap_aggr("empty");
	inp_tempmap_aggr *aggr;
	
	 //######################################################################################
	 //Management############################################################################
	 //######################################################################################
	 //croprot##############################################################################
		variables.clear();
		croprot=new Inp_croprot(
			IO_options["croprot"],				// decision map
			variables,				//vector <string> of variables used/needed
			"constant",				//Intervallmode
			1,						//multipli
			0.0001,					// duration
			"recent",				// "recent" or "longterm" not really relevant for croprot
			measurement_intervall,	// Intervall for which we would need the data
			root,					// directory of read/write file
			"croprot",				// name of read/write file
			suffix_inp				// suffix of read/write file
		);	
		if(croprot->set_input(
			croplist,			// list2d croplist is data source
			name,				//name for site identification in croplist
			plotprops)){		// plotprops_source
				err::err3("plot::set_inputs: Could not derive croprot data");
				delete (croprot);
				croprot=NULL;
		}
	//#####################################################################################
	//fertrot##############################################################################
		variables.clear();
		
		fertrot=new Inp_fertrot(
			IO_options["fertrot"],				// decision map
			variables,			//vector <string> of variables used/needed
			"constant",			//Intervallmode
			1,				    //multipli
			0.0001,				// duration
			"recent",			// "recent" or "longterm" not really relevant for croprot
			measurement_intervall,		// Intervall for which we would need the data
			root,				// directory of read/write file
			"fertrot",			// name of read/write file
			suffix_inp				// suffix of read/write file
		);	
		printf("bis hier ok\n");
		if(fertrot->set_input(
			fertlist,			// list2d fertlist is data source
			croprot,
			name,				//name for site identification in croplist
			plotprops)){		// plotprops_source
				err::err3("plot::set_inputs: Could not derive fertrot data");
				delete (fertrot);
				fertrot=NULL;
		}
	
	//########################################################################################
	// Measurements
	//n2o_daily###############################################################################
	
	variables.clear();
	variables.push_back("N2O_meas");
	n2o_day=new inp_measurements(
		IO_options["n2o_day"],
		variables,			//vector of variables used/needed
		"day",				//Intervallmode
		1,				    //multipli
		0.0001,				// duration
		"recent",			// "recent" or "longterm"
		measurement_intervall,		// Intervall for which we would need the data
		root,				// directory of read/write file
		name,			// name of read/write file
		".vwi"				// suffix of read/write file
		);
	if(n2o_day->set_input(
		motheraggr,			// aggregating mother input data
		plotprops)){			// read from plotprops
			err::err2("plot::set_inputs: Could not derive n2o_day data");
			delete (n2o_day);
			n2o_day=NULL;
		}
	if (n2o_day!=NULL)n2o_day->ptr->print_in_file_simple_style_with_mydate(n2o_day->root,
		"n2o_day",suffix_val);
	//#########################################################################################	
	//N2O_mon##################################################################################	
	
	variables.clear();
	variables.push_back("N2O_meas");
	n2o_mon=new inp_measurements(
		IO_options["n2o_mon"],
		variables,			//vector of variables used/needed
		"mon",				//Intervallmode
		1,				    //multipli
		0.0001,				// duration
		"recent",			// "recent" or "longterm"
		measurement_intervall,		// Intervall for which we would need the data
		root,				// directory of read/write file
		"n2o_mon",			// name of read/write file
		suffix_val			// suffix of read/write file
		);	
	if(n2o_day!=NULL){
		aggr=new inp_tempmap_aggr(n2o_day->ptr,"mean",0.3,2,variables);
		if(n2o_mon->set_input(
			aggr,			// source of aggregating mother input data
			plotprops)){		// plot_props_source
				err::err2("plot::set_inputs: Could not derive n2o_mon data");
				delete (n2o_mon);
				n2o_mon=NULL;
		}
		delete (aggr);
		}
	//#########################################################################################
	//N2O_year##################################################################################	
	
	variables.clear();
	variables.push_back("N2O_meas");
	n2o_year=new inp_measurements(
		IO_options["n2o_year"],
		variables,			//vector of variables used/needed
		"day",				//Intervallmode
		365,			    //multipli
		0.0001,				// duration
		"recent",			// "recent" or "longterm"
		measurement_intervall,		// Intervall for which we would need the data
		root,				// directory of read/write file
		"n2o_year",			// name of read/write file
		suffix_val				// suffix of read/write file
		);	
	if(n2o_day!=NULL){
		aggr=new inp_tempmap_aggr(n2o_day->ptr,"mean",0.3,2,variables);
	
	if(n2o_year->set_input(
		aggr,			// source of aggregating mother input data
		plotprops)){		// plot_props_source
			err::err3("plot::set_inputs: Could not derive n2o_year data");
			delete (n2o_year);
			n2o_year=NULL;
		}
	delete (aggr);
	}
	//######################################################################################
	//N2O_EF_site##################################################################################	

	variables.clear();
	variables.push_back("N2O_EF_site");
	n2o_EF_site=new inp_measurements(
		IO_options["n2o_EF_site"], 
		variables,			//vector of variables used/needed
		"day",				//Intervallmode
		365,			    //multipli
		0.0001,				// duration
		"recent",			// "recent" or "longterm"
		measurement_intervall,		// Intervall for which we would need the data
		root,				// directory of read/write file
		"n2o_EF_site",			// name of read/write file
		suffix_val				// suffix of read/write file
		);	
	if(n2o_EF_site->set_input(
		motheraggr,				// source of aggregating mother input data
		plotprops)){		// plot_props_source
			err::err2("plot::set_inputs: Could not derive n2o_EF_site data");
			delete (n2o_EF_site);
			n2o_EF_site=NULL;
			mother=NULL;
		}
	//######################################################################################
	//N2O_EF_plot##################################################################################	

	variables.clear();
	variables.push_back("N2O_EF_plot");
	n2o_EF_plot=new inp_measurements(
		IO_options["n2o_EF_plot"],
		variables,			//vector of variables used/needed
		"day",				//Intervallmode
		365,			    //multipli
		0.0001,				// duration
		"recent",			// "recent" or "longterm"
		measurement_intervall,		// Intervall for which we would need the data
		root,				// directory of read/write file
		"n2o_EF_plot",		// name of read/write file
		suffix_val				// suffix of read/write file
		);	
	if(n2o_EF_plot->set_input(
		motheraggr,				// source of aggregating mother input data
		plotprops)){		// plot_props_source
			err::err2("plot::set_inputs: Could not derive n2o_EF_site data");
			delete (n2o_EF_plot);
			n2o_EF_plot=NULL;
			mother=NULL;
		}
	//######################################################################################
	
	//######################################################################################
	//Weather_Input#########################################################################
	//######################################################################################
	//monthly recent weather data###########################################################
	variables.clear();
	variables.push_back("P");
	variables.push_back("T");
	variables.push_back("R");
	inp_weather=new Inp_weather(
		IO_options["inp_weather"],
		variables,			//vector of variables used/needed
		"mon",				//Intervallmode
		1,				    //multipli
		0.0001,				// duration
		"recent",			// "recent" or "longterm"
		measurement_intervall,			// Intervall for which we would need the data
		root,				// directory of read/write file
		"inp_weather",				// name of read/write file
		suffix_inp				// suffix of read/write file
		);			
	 	
	if(inp_weather->set_input(
		motheraggr,
		plotprops)){			// aggregating mother input data
			err::err3("plot::set_inputs: Could not derive met_mon data");
			delete (inp_weather);
			inp_weather=NULL;
		}
	//######################################################################################
	//monthly longterm weather data###########################################################

	variables.clear();
	variables.push_back("P");
	variables.push_back("T");
	variables.push_back("R");
	timestamp start_lt(measurement_intervall.get_start().get_year(),1,1.0); 
	timestamp end_lt(measurement_intervall.get_start().get_year(),12,3.0);
	time_intervall temp_int(start_lt,end_lt);

	inp_weather_lon=new Inp_weather(
		IO_options["inp_weather_lon"],	//cont
		variables,			//vector of variables used/needed
		"mon",				//Intervallmode
		1,				    //multipli
		0.0001,				// duration
		"longterm",			// "recent" or "longterm"
		temp_int,			// Intervall for which we would need the data
		root,				// directory of read/write file
		"inp_weather_lon",		// name of read/write file
		suffix_inp			// suffix of read/write file
		);			
	 	
	if(inp_weather_lon->set_input(
		motheraggr,
		plotprops)){			// aggregating mother input data
			err::err3("plot::set_inputs: Could not derive inp_weather_lon data");
			delete (inp_weather_lon);
			inp_weather_lon=NULL;
		}
	//###################################################################################
	// time series for initialisation
	return 0;
}	
 int plot::set_model_environment()
	{
		//tempgrid_lt
		vector <string> nullvec;
		tempgrid_lt= new tempmap <string,double>(0,-999999,"mon",1,1);
		tempgrid=new tempmap <string,double>(0,-99999,"constant",1,1);
		tempgrid_lt->set_an_empty_tempmap(inp_weather_lon->ptr,nullvec);
		tempgrid->set_an_empty_tempmap(inp_weather->ptr,nullvec);
		
		//horizons
		vector <int> hori;
		vector <int> *horizons;
		int x;
		for(x=0;x<10;x++)
		{
			hori.push_back((x+1)*10);
		}
		horizons=&hori;
		
// declare inputs
		 input_grids=new Mod_in(tempgrid,horizons,this->get_root());
// define Mode_out_handling
		
		 
			
                
// declare all models used;
 		
//ETP
		mod_etp=new Mod_ETP_Turc(
				IO_options["mod_etp"],
				input_grids,
				inp_weather,
				mod_interception,
				mod_crop,
				mod_wc,
				mod_surface_water,
				mod_snow
				);
		
		//etp_mod_lon=new Mod_ETP_Turc(	
		//		IO_options["mod_etp_lon"],
		//		input_grids_lon,			//tempgrid
		//		inp_weather_lon,			  // weather
		//		mod_interception_lon,
		//		mod_crop_lon,
		//		mod_wc_lon,
		//		mod_surface_water_lon,
		//		mod_snow_lon
		//		);
		
///////////////////////////////////////////////////////////////////////////////////////////
// Snowmodel
		compute_snow_growth= new Compute_Snow_growth;
		compute_snow_melt=new Compute_Snow_melt_Tagesgradverfahren();
		mod_snow=new Mod_Snow_simple(
				IO_options["mod_snow"],
				input_grids,
				inp_weather,
				compute_snow_melt,
				mod_etp,
				compute_snow_growth
				);
/////////////////////////////////////////////////////////////////////////////////////////
// Interception
	mod_interception=new Mod_Interception_simple(
				IO_options["mod_interception"],
				input_grids,
				inp_weather,
				mod_etp,
				mod_crop
				);
//////////////////////////////////////////////////////////////////////////////////////////
//	Surface_water
	 mod_surface_water=new Mod_Surface_water_simple(
				IO_options["mod_surface_water"],
				input_grids,
				inp_weather,
				mod_etp,
				mod_interception,
				mod_snow,
				mod_wc
				);
//////////////////////////////////////////////////////////////////////////////////////////////
// wc
	mod_wc=new Mod_wc_fc(
				IO_options["mod_wc"],
				input_grids,
				mod_pdf,
				mod_etp,
				mod_soiltemp,
				mod_surface_water,
				mod_crop);
////////////////////////////////////////////////////////////////////////////////////////////////
// pdf
	mod_pdf=new Mod_PDF_Hydraulics_KA5(
				IO_options["mod_pdf"],
				input_grids,
				mod_soilphysics,
				mod_wc);
///////////////////////////////////////////////////////////////////////////////////////////////////////
// soilphysics
	mod_soilphysics=new Mod_Soilphysics_from_top_layer(
				IO_options["mod_pdf"],
				input_grids,
				plotprops["sand"],
				plotprops["silt"],
				plotprops["clay"],
				plotprops["bd"],
				plotprops["ph"],
				plotprops["soc"],
				plotprops["ntot"]);
	/////////////////////////////////////////////////////////////////////////////////////////////////////
				
				
				
	
		
		return 0;
	}
		
	




string plot::get_root()
{
	return root;
}
//int plot::check_for_ETP()
//{
//	int dec=0;
//	unsigned int row;
//	double dum;
//	string striloc;
//	map<const string,vector<double> >::const_iterator it;
//
//	for(it=weather.val.begin();it!=weather.val.end();it++){
//		striloc=it->first;
//		if(striloc.compare("ETP")==0)dec=1;
//	}
//	if(dec==1)return 0;
//	for(row=0;row<weather.time.size();row++)
//	{
//		dum=Basics_weather::get_monthly_ETP(weather.val["T"][row],weather.val["R"][row],Basics::get_mondur(weather.time[row].getyear(),weather.time[row].getmon()));
//		weather.val["ETP"].push_back(dum);
//	}
//	weather.print_file(root,name,".tdi");
//	return 0;
//}

//int plot::check_for_soil_water_KA4()
//	{
//		const string WC_KA4="WC_KA4";
//		if(Basics_map::check_for_identifier_occurence(weather.val,WC_KA4)<1){return 0;}
//		else{
//			
//			Basics_weather::get_bucket_storage(weather.val,"P","ETP","WC_KA4",plotprops["WC_PWP"]*300,this->plotprops["WC_FC"]*300);
//			weather.print_file(root,name,".tdi");
//		}
//		return 0;
//	}
////
////int plot::read_measurements(int NEW_VMI_FILES)
////{
//    if(NEW_VMI_FILES!=0) //yes generate in every case new vmi data
//        {
//            cout<<this->root<<this->name<<endl;
//            if((daymeas.read_tempmap(this->root,this->name,".vwi"))==0)
//            {
//                this->daymeas.check_for_consistency();
//                this->monmeas.accumulate_into_monthly(this->daymeas);
//				this->monmeas.print_file(root,name,".vmi");
//            }
//            else return 1;
//        }
//        else{
//                if( this->monmeas.read_tempmap(this->root,this->name,".vmi")==0)
//                        this->monmeas.check_for_consistency();
//                else{
//                     if((this->daymeas.read_tempmap(this->root,this->name,".vwi"))==0)
//                     {
//                        this->daymeas.check_for_consistency();
//                        this->monmeas.accumulate_into_monthly(this->daymeas);
//						this->monmeas.print_file(root,name,".vmi");
//                     }
//                     else return 1;
//                }
//        }
//    return 0;
//}	
//
//
